package com.util;

import org.springframework.util.DigestUtils;

public class PasswordDecode {
    private static int[] char_array = {'0', '1', '2', '3','4', '5', '6', '7', '8', '9'};
    public  static  String encryption(String password) {
          //加密次数
        int index_count=(int) (Math.random()*char_array.length);
        //加密盐值
        int index_salt=(int) (Math.random()*char_array.length);
       String result = password;
        for(int i=0;i<char_array[index_salt];i++){
          result=result+(char)char_array[index_salt];
          result=DigestUtils.md5DigestAsHex(result.getBytes());
        }
        result=(char) char_array[index_count]+result+(char)char_array[index_salt];

      return result;

    }



    public static boolean decode(String password,String decode) {

        char char_count=decode.charAt(0);

        char char_salt=decode.charAt(decode.length()-1);
        for(int i=0;i<char_count;i++) {
           password += char_salt;


            password = DigestUtils.md5DigestAsHex(password.getBytes());
        }
            return decode.equals(char_count+password+char_salt);

    }

}
